//
//  InputVoiceView.m
//  translateAi
//
//  Created by 陈镇 on 2023/5/7.
//

#import "InputVoiceView.h"
#import "CZAVAudioManager.h"
//#import "BuyVipPageVC.h"
#import "RecordAnimateView.h"
#import "SLUserPermissionsManager.h"
#import "SLPopMenuView.h"
#import "LanguageSelectView.h"

@interface InputVoiceView ()<CZAVAudioManagerDelegate>
/** <#注释#>*/
@property (nonatomic, strong) QMUIButton * leftBtn;
@property (nonatomic, strong) QMUIButton * leftBtn_xiala;
/** <#注释#>*/
@property (nonatomic, strong) QMUIButton * rightBtn;
@property (nonatomic, strong) QMUIButton * rightBtn_xiala;

/** <#注释#>*/
@property (nonatomic, strong) QMUILabel * tipsvoiceWave;
/** <#注释#>*/
@property (nonatomic, strong) RecordAnimateView * voiceWave;


@property(nonatomic,strong) QMUILabel *tips;

/** <#注释#>*/
@property (nonatomic, strong) QMUILabel * inputContent;
/** <#注释#>*/
@property (nonatomic, strong) UIImageView * doneImage;

/** 是否在说话*/
@property (nonatomic, assign) InputVoiceViewType type;

/** <#注释#>*/
@property (nonatomic, strong) QMUIButton * currentBtn;

@property (nonatomic, strong) NSString * leftDefaultTitle;
@property (nonatomic, strong) NSString * rightDefaultTitle;

//缓存得到的录音文件
@property (nonatomic, strong) NSString * voiceBase64;

@end
@implementation InputVoiceView


-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        self.backgroundColor = UIColorHex(#EFF0F8FF);
        
        
        self.or_code = @"zh-CHS";
        self.ta_code = @"en";
        
        self.leftDefaultTitle = @"中文";
        self.rightDefaultTitle = @"英语";
        
        [self addSubview:self.tipsvoiceWave];
        [self addSubview:self.voiceWave];
        
        [self addSubview:self.inputContent];
        [self addSubview:self.doneImage];
        
        self.type = InputVoiceViewTypeDefault;
        

        
    }
    return self;
}

-(QMUILabel *)tips{
    if (!_tips) {
        _tips = [QMUILabel createTextColor:UIColorHex(AAAAAA) font:SLFontMakeRegular(16) textAlignment:NSTextAlignmentCenter];
        _tips.text = @"请按住上方按钮开始说话";

    }
    return _tips;
}
-(QMUILabel *)inputContent{
    if(!_inputContent){
        _inputContent = [QMUILabel createTextColor:UIColorHex(#333333FF) font:SLFontMakeBold(16) textAlignment:NSTextAlignmentCenter];
        _inputContent.frame = CGRectMake(20, 24, self.width - 40, 70);
        _inputContent.text = @"录音完成，点击下方翻译按钮";
    }
    return _inputContent;
}
-(UIImageView *)doneImage{
    if(!_doneImage){
        _doneImage = [[UIImageView alloc] initWithFrame:CGRectMake(40, 150, self.width - 80, 7)];
        _doneImage.image = [UIImage imageNamed:@"input_end_icon"];
    }
    return _doneImage;
}
-(RecordAnimateView *)voiceWave{
    if(!_voiceWave){
        _voiceWave = [[RecordAnimateView alloc] initWithFrame:CGRectMake((self.width - 240)/2, 107, 240, 66)];
        _voiceWave.image = [UIImage imageNamed:@"voice_wave"];
    }
    return _voiceWave;
}
-(QMUILabel *)tipsvoiceWave{
    if(!_tipsvoiceWave){
        _tipsvoiceWave = [QMUILabel createTextColor:UIColorHex(333333) font:SLFontMakeBold(18) textAlignment:NSTextAlignmentCenter];
        _tipsvoiceWave.frame = CGRectMake(0, 24, self.width, 25);
        _tipsvoiceWave.text = @"正在录音...";
    }
    return _tipsvoiceWave;
}
-(QMUIButton *)leftBtn{
    if(!_leftBtn){
        
        CGFloat width = (self.width - 40)/2;
//        _leftDefaultTitle = @"按住说话";
        _leftBtn = [QMUIButton createButtonTitle:@"按住说话" titleColor:UIColorWhite font:SLFontMakeBold(14) imageName:@"voice_icon" selectImageName:@"voice_icon"];
        _leftBtn.spacingBetweenImageAndTitle = 5;
        _leftBtn.imagePosition = QMUIButtonImagePositionLeft;
        
    
        _leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
        _leftBtn.frame = CGRectMake(20, self.height - 35 - 48, width, 48);
        
        _leftBtn.backgroundColor = UIColorHex(#FCCC39FF);
        
        [_leftBtn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
        [_leftBtn addTarget:self action:@selector(touchOutside:) forControlEvents:UIControlEventTouchUpOutside];
        [_leftBtn addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [_leftBtn addTarget:self action:@selector(touchDragInside:) forControlEvents:UIControlEventTouchDragInside];
        [_leftBtn addTarget:self action:@selector(touchDragOutside:) forControlEvents:UIControlEventTouchDragOutside];
        
        
        UIImage *xiala = [[UIImage imageNamed:@"xiala"] qmui_imageWithTintColor:UIColorWhite];
        _leftBtn_xiala = [[QMUIButton alloc] init];
        [_leftBtn_xiala setImage:xiala forState:UIControlStateNormal];
    
        [_leftBtn addSubview:_leftBtn_xiala];
        [_leftBtn_xiala addTarget:self action:@selector(clickXialaItem:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _leftBtn;
}
-(QMUIButton *)rightBtn{
    if(!_rightBtn){
//        _rightDefaultTitle = @"按住说话";
        _rightBtn = [QMUIButton createButtonTitle:_rightDefaultTitle titleColor:UIColorWhite font:SLFontMakeBold(14) imageName:@"voice_icon" selectImageName:@"voice_icon"];
        _rightBtn.spacingBetweenImageAndTitle = 5;
        _rightBtn.imagePosition = QMUIButtonImagePositionLeft;
        _rightBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
        _rightBtn.frame = CGRectMake(_leftBtn.right - 1, _leftBtn.top, _leftBtn.width, _leftBtn.height);

        _rightBtn.backgroundColor = UIColorHex(#43ADFFFF);

        [_rightBtn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
        [_rightBtn addTarget:self action:@selector(touchOutside:) forControlEvents:UIControlEventTouchUpOutside];
        [_rightBtn addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIImage *xiala = [[UIImage imageNamed:@"xiala"] qmui_imageWithTintColor:UIColorWhite];
        _rightBtn_xiala = [[QMUIButton alloc] init];
        [_rightBtn_xiala setImage:xiala forState:UIControlStateNormal];
 
        [_rightBtn addSubview:_rightBtn_xiala];
        [_rightBtn_xiala addTarget:self action:@selector(clickXialaItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}
- (void)setType:(InputVoiceViewType)type{
    _type = type;
    self.tipsvoiceWave.hidden = YES;
    self.voiceWave.hidden = YES;
    
    self.inputContent.hidden = YES;
    self.doneImage.hidden = YES;
    
    switch (type) {
        case InputVoiceViewTypeDefault:
            [self.leftBtn setTitle:_leftDefaultTitle forState:UIControlStateNormal];
            [self.rightBtn setTitle:_rightDefaultTitle forState:UIControlStateNormal];
            self.hidden = YES;
            self.tips.hidden = NO;
            
            [_rightBtn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
            [_rightBtn addTarget:self action:@selector(touchOutside:) forControlEvents:UIControlEventTouchUpOutside];
            
            
            [_leftBtn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
            [_leftBtn addTarget:self action:@selector(touchOutside:) forControlEvents:UIControlEventTouchUpOutside];
            
            break;
        case InputVoiceViewTypeInput:
            
            self.tipsvoiceWave.hidden = NO;
            self.voiceWave.hidden = NO;
            self.hidden = NO;
            self.tips.hidden = YES;
            break;
        case InputVoiceViewTypeInputEnd:
            
            self.inputContent.hidden = NO;
            self.doneImage.hidden = NO;
            
            [self.leftBtn setTitle:@"点击翻译" forState:UIControlStateNormal];
            [self.rightBtn setTitle:@"Translation" forState:UIControlStateNormal];
            self.hidden = NO;
            self.tips.hidden = YES;
            
            [_rightBtn removeTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
            [_rightBtn removeTarget:self action:@selector(touchOutside:) forControlEvents:UIControlEventTouchUpOutside];
            
            [_leftBtn removeTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
            [_leftBtn removeTarget:self action:@selector(touchOutside:) forControlEvents:UIControlEventTouchUpOutside];
            
            
            break;
            
        default:
            break;
    }
    
}

-(void)btnToView:(UIView *)superView{
    [superView addSubview:self.leftBtn];
    [superView addSubview:self.rightBtn];
    [superView addSubview:self.tips];
    CGFloat width = (superView.width - 60 -26)/2;
    _leftBtn.frame = CGRectMake(30, superView.height - 35 - 48 - TabBarHeight, width, 48);
    _rightBtn.frame = CGRectMake(_leftBtn.right + 26, _leftBtn.top, _leftBtn.width, _leftBtn.height);
    _leftBtn_xiala.frame = CGRectMake(_leftBtn.width - 30, 0, 30, _leftBtn.height);
    _rightBtn_xiala.frame = CGRectMake(_rightBtn.width -  30, 0, 30, _rightBtn.height);
    
    _leftBtn.layer.masksToBounds = YES;
    _leftBtn.layer.cornerRadius = _leftBtn.height/2;
    
    _rightBtn.layer.masksToBounds = YES;
    _rightBtn.layer.cornerRadius = _leftBtn.height/2;
    
    _tips.frame = CGRectMake(0, _leftBtn.bottom + 5, self.width, 25);
}

-(void)hiddenbuttom:(BOOL)hidden{
    self.leftBtn.hidden = hidden;
    self.rightBtn.hidden = hidden;
    self.tips.hidden = hidden;
}
-(void)changeLeftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle{

    _leftDefaultTitle = leftTitle;
    _rightDefaultTitle = rightTitle;
    [self.leftBtn setTitle:leftTitle forState:UIControlStateNormal];
    
    [self.rightBtn setTitle:rightTitle forState:UIControlStateNormal];
}

#pragma mark - Action
-(void)touchDragInside:(QMUIButton *)btn{
    _tipsvoiceWave.text = @"正在录音...";
}
-(void)touchDragOutside:(QMUIButton *)btn{
    _tipsvoiceWave.text = @"放弃录音";
}

-(void)touchDown:(QMUIButton *)btn{
    
    
    AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    
    if (videoAuthStatus == AVAuthorizationStatusNotDetermined) {

        [SLUserPermissionsManager isHaveAudioPermissionsBlock:^(BOOL isCan) {
          
            [[CZAVAudioManager shareManager] abandonRecord];
        }];
        return;
    }

    __weak typeof(self) weakSelf = self;
    [SLUserPermissionsManager isHaveAudioPermissionsBlock:^(BOOL isCan) {
        if (isCan) {
            weakSelf.currentBtn = btn;
            weakSelf.type = InputVoiceViewTypeInput;
            RTLog(@"开始录音");
            [[CZAVAudioManager  shareManager] startRecordDelegate:weakSelf];
        }else{
            [weakSelf touchOutside:nil];
        }

    }];


    
}
-(void)touchUpInside:(QMUIButton *)btn{
    AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    
    if (videoAuthStatus == AVAuthorizationStatusNotDetermined) {
        return;
    }
    if(self.type == InputVoiceViewTypeInputEnd){
        RTLog(@"开始翻译 - 翻译结束");
        self.type = InputVoiceViewTypeDefault;
        //不管点那个开始翻译，都是开始录音时候选的语言，按钮
        
        NSLog(@"%@",_currentBtn.titleLabel.text);
        
        if(_delegate && [_delegate respondsToSelector:@selector(translateCompleteDataVoiceBase64:isLeft:)]){
            int isChinese = 1;
            if(_currentBtn == _leftBtn){
                isChinese = 1;
            }else{
                isChinese = 0;
            }
            if(_voiceBase64.length  == 0){
                return;
            }
            [_delegate translateCompleteDataVoiceBase64:_voiceBase64 isLeft:isChinese];
        }
        return;
    }
    
    self.type = InputVoiceViewTypeInputEnd;
    RTLog(@"结束录音");
    [[CZAVAudioManager shareManager] endRecord];
}
-(void)touchOutside:(QMUIButton *)btn{
    self.type = InputVoiceViewTypeDefault;
    RTLog(@"放弃录音");
    [[CZAVAudioManager shareManager] abandonRecord];
    
}

//得到音频basec6
-(void)audioRecorderDidFinishvoiceBase64:(NSString *)voiceBase64 isForceEnd:(BOOL)isForceEnd{
    _voiceBase64 = voiceBase64;
    
    if(isForceEnd){
        //kMaxTimeLength
        NSString *tips = [NSString stringWithFormat:@"录音时长不能超过%@秒",@(kMaxTimeLength)];
        SLTipCurrenView(tips);
        self.currentBtn.highlighted = NO;
    }
}

-(void)audioRecorderUnderTime{
    SLTipCurrenView(@"录音时间太短");
    self.currentBtn.highlighted = NO;
    self.type = InputVoiceViewTypeDefault;
}




-(void)clickXialaItem:(QMUIButton *)btn{
 

    __weak typeof(self) weakSelf = self;
    [CurrentVC.view endEditing:YES];
    [SLPopMenuView showMenuWithView:btn titles:[LanguageSelectView mylanguageList:@"text"] imageNames:@[] block:^(XHPopMenuItem * _Nullable item) {
            
        
        if (btn == weakSelf.leftBtn_xiala) {
            weakSelf.leftDefaultTitle = item.title;
            [weakSelf.leftBtn setTitle:item.title forState:UIControlStateNormal];
            NSString *ta_code = [ LanguageSelectView getLanguageCode:item.title type:@"text"];
            weakSelf.or_code = ta_code;
        }else{
            weakSelf.rightDefaultTitle = item.title;
            [weakSelf.rightBtn setTitle:item.title forState:UIControlStateNormal];
            NSString *ta_code = [ LanguageSelectView getLanguageCode:item.title type:@"text"];
            weakSelf.ta_code = ta_code;
        }
     
     
  

    }];
    
    
}

@end
